﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using UI.Base;
using UI.MainFunctions;
using UI.MasterData;
using UI.Reports;

namespace UI
{
    public partial class MainForm : DialogViewBase
    {
        #region Variables

        private readonly List<Form> _childForms;


        #endregion

        #region Constructor

        public MainForm ()
        {
            _childForms = new List<Form>();
            InitializeComponent();
        }

        #endregion

        #region Events

        // transaction

        /// <summary>
        /// Show Goods Receive Note form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <history>
        /// 11/16/2014 phat.vo: Create new
        /// </history>
        private void mnuGRNItem_Click (object sender, EventArgs e)
        {
            AddChildMdi<frmStockInManager>();
            //var frmStockInManager = new frmStockInManager
            //                            {
            //                                MdiParent = this
            //                            };
            //frmStockInManager.Show();
        }

        /// <summary>
        /// Show Outsourcing Order form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <history>
        /// 11/16/2014 phat.vo: Create new
        /// </history>
        private void mnuOutsourcingItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmTransferManager>();
            //var frmTransferManager = new frmTransferManager
            //                             {
            //                                 MdiParent = this
            //                             };
            //frmTransferManager.Show();
        }

        /// <summary>
        /// Show Delivery Order form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <history>
        /// 11/16/2014 phat.vo: Create new
        /// </history>
        private void mnuDeliveryOrderItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmStockOutManager>();
            //var frmStockOutManager = new frmStockOutManager
            //                             {
            //                                 MdiParent = this
            //                             };
            //frmStockOutManager.Show();
        }

        /// <summary>
        /// Show ReceiptManager form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <history>
        /// 11/16/2014 phat.vo: Create new
        /// </history>
        private void mnuReceptItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmReceiptManager>();
            //var frmReceiptManager = new frmReceiptManager
            //                            {
            //                                MdiParent = this
            //                            };
            //frmReceiptManager.Show();
        }

        // master data

        /// <summary>
        /// Show Customer manage form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuCustomerItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmCustomerManager>();
            //var frmCustomerManager = new frmCustomerManager
            //{
            //    MdiParent = this
            //};
            //frmCustomerManager.Show();
        }

        private void mnuProductBrandItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmProductCategoryManager>();
            //var frmProductCategoryManager = new frmProductCategoryManager
            //                                    {
            //                                        MdiParent = this
            //                                    };
            //frmProductCategoryManager.Show();
        }

        private void mnuProductPropertyItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmProductAttribManager>();
            //var frmProductAttribManager = new frmProductAttribManager
            //{
            //    MdiParent = this
            //};
            //frmProductAttribManager.Show();
        }

        private void mnuProductPackageItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmPackageTypeManager>();
            //var frmPackageTypeManager = new frmPackageTypeManager
            //{
            //    MdiParent = this
            //};
            //frmPackageTypeManager.Show();
        }

        private void mnuProductUnitItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmUnitOfMeasureManager>();
            //var frmUnitOfMeasureManager = new frmUnitOfMeasureManager
            //{
            //    MdiParent = this
            //};
            //frmUnitOfMeasureManager.Show();
        }

        // report region

        private void mnuGeneralStorageItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmInventoryReport>();
            //var frmInventoryReport = new frmInventoryReport
            //                             {
            //                                 MdiParent = this
            //                             };
            //frmInventoryReport.Show();
        }

        private void mnuDetailStorageItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmInventoryItemReport>();
            //var frmInventoryItemReport = new frmInventoryItemReport
            //                                 {
            //                                     MdiParent = this
            //                                 };
            //frmInventoryItemReport.Show();
        }

        private void mnuGeneralLiabilityItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmGeneralLeague>();
            //var frmGeneralLeague = new frmGeneralLeague
            //                           {
            //                               MdiParent = this
            //                           };
            //frmGeneralLeague.Show();
        }

        private void mnuDetailLiabilityItem_Click (object sender, System.EventArgs e)
        {
            AddChildMdi<frmCustomerLiability>();
            //var frmCustomerLiability = new frmCustomerLiability
            //                               {
            //                                   MdiParent = this
            //                               };
            //frmCustomerLiability.Show();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Add form to MDI Child node
        /// </summary>
        /// <typeparam name="T">Form type</typeparam>
        /// <history>
        /// 11/16/2014 phat.vo: Create new
        /// </history>
        private void AddChildMdi<T> () where T : Form
        {
            // Open new form if there is no same form
            // otherwise, active form
            var type = typeof(T);
            var instance = (T) Activator.CreateInstance(type);
            var form = instance as Form;

            if(_childForms.Exists(x => x.Name == form.Name))
            {
                var childForm = _childForms.Find(x => x.Name == form.Name);
                childForm.Activate();
            }
            else
            {
                form.FormClosed += FormClosedEvent;
                form.MdiParent = this;
                _childForms.Add(form);
                form.Show();
            }
        }

        private void FormClosedEvent(object sender, FormClosedEventArgs e)
        {
            var form = sender as Form;
            if (form!= null && _childForms.Exists(x => x.Name == form.Name))
            {
                _childForms.Remove(_childForms.FirstOrDefault(x => x.Name == form.Name));
            }
        }

        #endregion
    }
}